package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.ICompletionOrderListForMapView;
import com.cloudsigns.factorymanager2.engineering.IView.InEngineeringOrderListForMapView;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringFeedBackForCompletionStateActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringFeedBackForExceptionStateActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringFeedBackForPauseStateActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallFeedBackForWaitStateActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringFeedBackForWorkingStateActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallOrderForSuperActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringMainForManagerActivity;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.CompletionOrderListForMapPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringOrderListForMapPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/3/3.
 */

public class MapForManagerFragment extends Fragment implements InEngineeringOrderListForMapView, ICompletionOrderListForMapView,BottomDialog.OnBottomDialogClick {
    private final String TAG = MapForManagerFragment.class.getSimpleName();
    private MapView bmapView;
    private BaiduMap mBaiduMap;
    //    private List<Double> latList;
//    private List<Double> lonList;
    private EngineeringOrderListForMapPresenter engineeringOrderListForMapPresenter;
    private List<OrderParentBean> engineeringOrderList;
    private CompletionOrderListForMapPresenter completionOrderListForMapPresenter;
    private List<OrderParentBean> completionOrderList;
    private int currentModuleType = -1;
//    private LocationService locService;

    //"http://host:port/commonConstructDept/orderList  [ [通用]获取工程部工单列表-给地图用 ]

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map_for_manager, container, false);

        //获取地图控件引用
        bmapView = view.findViewById(R.id.bmapView);
        //不显示缩放控件
        bmapView.showZoomControls(false);

        /*获取地图对象*/
        mBaiduMap = bmapView.getMap();

//        latList = new ArrayList<>();
//        latList.add(43.88);
//        latList.add(43.87);
//        latList.add(44.45);
//        latList.add(44.52);
//        latList.add(44.83);
//        latList.add(44.15);
//        latList.add(43.53);
//        latList.add(43.87);
//        latList.add(44.4);
//        latList.add(43.75);
//        latList.add(42.97);
//        latList.add(42.93);
//        latList.add(42.93);
//        latList.add(43.32);
//        latList.add(42.85);
//        latList.add(42.98);
//
//        lonList = new ArrayList<>();
//        lonList.add(125.35);
//        lonList.add(126.57);
//        lonList.add(125.15);
//        lonList.add(125.68);
//        lonList.add(126.55);
//        lonList.add(126.83);
//        lonList.add(125.68);
//        lonList.add(126.57);
//        lonList.add(126.97);
//        lonList.add(127.33);
//        lonList.add(126.72);
//        lonList.add(126.03);
//        lonList.add(129.52);
//        lonList.add(129.75);
//        lonList.add(130.35);
//        lonList.add(129.83);

        resetMapView();
        mBaiduMap.setOnMarkerClickListener(onMarkerClickListener);

        engineeringOrderListForMapPresenter = new EngineeringOrderListForMapPresenter(this);
        completionOrderListForMapPresenter = new CompletionOrderListForMapPresenter(this);
        return view;
    }

    /*设置地图默认显示坐标点和缩放级别*/
    private void resetMapView() {
        int zoom = 5;
        double latDefault = 34.0;
        double lngDefault = 108.0;

        try {
            if (mBaiduMap != null) {
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().target(new LatLng(latDefault, lngDefault)).zoom(zoom).build()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /*选择一个工程后设置以该项目为中心的地图显示*/
    public void selectEngineering(double lat, double lng) {

        LatLng latLng = new LatLng(lat, lng);
        if (mBaiduMap != null) {
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).target(latLng).build()));
        }
    }


//    /*设置地图默认显示坐标点和缩放级别*/
//    private void resetMapView() {
//        float zoom = 11.0f;
//        double latDefault = 43.88;
//        double lngDefault = 125.25;
//        try {
//            if (mBaiduMap != null) {
//                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().target(new LatLng(latDefault, lngDefault)).zoom(zoom).build()));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public void getOrderList(int engineeringType) {
        resetMapView();

        currentModuleType = engineeringType;
        mBaiduMap.clear();

        if (currentModuleType == EngineeringMainForManagerActivity.MODULE_ENGINEERING_MANAGEMENT) { //工程管理
            engineeringOrderListForMapPresenter.getEngineeringOrderList();

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_TRACK) { //轨迹
            completionOrderListForMapPresenter.getCompletionOrderListForMap();

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_INTELLIGENT) { //智能控制

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_YEJI) { //业绩
          completionOrderListForMapPresenter.getCompletionOrderListForMap();
        }
// else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_GONGXU) { //供需
//
//        }

    }


    private void updataMap() {

        if (currentModuleType == EngineeringMainForManagerActivity.MODULE_ENGINEERING_MANAGEMENT) { //工程管理
            if (null != engineeringOrderList) {
                addMarker(engineeringOrderList);
            }

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_TRACK) { //轨迹

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_INTELLIGENT) { //智能控制

        } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_YEJI) { //业绩
            if (null != completionOrderList) {
                addMarkerForYeji(completionOrderList);
            }
        }
//        else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_GONGXU) { //供需
//
//        }


    }

    /*往地图添加一个点*/

    private void addMarker(List<OrderParentBean> orderParentBeanList) {

        //创建OverlayOptions的集合

        List<OverlayOptions> options = new ArrayList<OverlayOptions>();
        //构建Marker图标
        BitmapDescriptor bitmap_exception = null;
        BitmapDescriptor bitmap_wait_confirm = null;
        BitmapDescriptor bitmap_working = null;
        BitmapDescriptor bitmap_complete = null;
        BitmapDescriptor bitmap_out_plan = null;
        BitmapDescriptor bitmap_no_feedback = null;
        BitmapDescriptor bitmap_pause = null;
        BitmapDescriptor bitmap_create = null;

        LatLng point;
        Bundle bundle = null;
        String title = "";
        BitmapDescriptor bitmap = null;

        for (int i = 0; i < orderParentBeanList.size(); i++) {
            bitmap = null;

            OrderParentBean orderParentBean = orderParentBeanList.get(i);

            bundle = new Bundle();
            bundle.putSerializable(ConstantUtil.KEY_1, orderParentBean);

            CommonEnum.EngineeringORderCategory oRderCategory = orderParentBean.getCategory();
            if (null != oRderCategory) {
                if (oRderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                    title = "工程安装";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                    title = "批量工程";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                    title = "施工";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                    title = "维修";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                    title = "上刊";
                    ;
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.PATROL) {
                    title = "巡查";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.SURVEY) {
                    title = "勘查";
                }
            }

//            //设置坐标点
//            int j = i % 16;
//            point = new LatLng(latList.get(j), lonList.get(j));
            point = new LatLng(orderParentBean.getLat(), orderParentBean.getLon());


            OverlayOptions option = null;

            CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
            if (CommonEnum.FeedBackState.EXCEPTION == feedBackState) {
                if (null == bitmap_exception) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_2);

                    //构建Marker图标
                    bitmap_exception = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }

                bitmap = bitmap_exception;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);


            } else if (CommonEnum.FeedBackState.WAIT_CONFIRM == feedBackState) {
                if (null == bitmap_wait_confirm) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_1);
                    //构建Marker图标
                    bitmap_wait_confirm = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }

                bitmap = bitmap_wait_confirm;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.WORKING == feedBackState) {
                if (null == bitmap_working) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_3);
                    //构建Marker图标
                    bitmap_working = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_working;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.COMPLETE == feedBackState) {
                if (null == bitmap_complete) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_7);
                    //构建Marker图标
                    bitmap_complete = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_complete;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.OUT_OF_PLAN == feedBackState) {
                if (null == bitmap_out_plan) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_4);
                    //构建Marker图标
                    bitmap_out_plan = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_out_plan;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.NO_FEEDBACK == feedBackState) {
                if (null == bitmap_no_feedback) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_5);
                    //构建Marker图标
                    bitmap_no_feedback = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_no_feedback;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.PAUSE == feedBackState) {
                if (null == bitmap_pause) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_6);
                    //构建Marker图标
                    bitmap_pause = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_pause;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.CREATE == feedBackState) {
                if (null == bitmap_create) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_8);
                    //构建Marker图标
                    bitmap_create = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_create;
            }


            //创建OverlayOptions属性
            if (null != bitmap)
                option = new MarkerOptions()
                        .position(point)
                        .title(title)
                        .extraInfo(bundle)
                        .icon(bitmap);


            //将OverlayOptions添加到list
            if (null != option)
                options.add(option);


        }


        //在地图上批量添加
        mBaiduMap.addOverlays(options);

    }


    /*往地图添加一个点*/

    private void addMarkerForYeji(List<OrderParentBean> orderParentBeanList) {

        //创建OverlayOptions的集合

        List<OverlayOptions> options = new ArrayList<OverlayOptions>();
        //构建Marker图标
        BitmapDescriptor bitmap_icon = null;


        LatLng point;
        Bundle bundle = null;
        String title = "";


        for (int i = 0; i < orderParentBeanList.size(); i++) {

            OrderParentBean orderParentBean = orderParentBeanList.get(i);

            bundle = new Bundle();
            bundle.putSerializable(ConstantUtil.KEY_1, orderParentBean);


//            //设置坐标点
//            int j = i % 16;
//            point = new LatLng(latList.get(j), lonList.get(j));
            point = new LatLng(orderParentBean.getLat(), orderParentBean.getLon());


            OverlayOptions option = null;



                if (null == bitmap_icon) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_2);

                    //构建Marker图标
                    bitmap_icon = BitmapDescriptorFactory
                            .fromBitmap(tmpBitmap);
                }



            //创建OverlayOptions属性
            if (null != bitmap_icon)
                option = new MarkerOptions()
                        .position(point)
                        .title(title)
                        .extraInfo(bundle)
                        .icon(bitmap_icon);


            //将OverlayOptions添加到list
            if (null != option)
                options.add(option);


        }


        //在地图上批量添加
        mBaiduMap.addOverlays(options);

    }

    BaiduMap.OnMarkerClickListener onMarkerClickListener = new BaiduMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(final Marker marker) {
            Bundle bundle = marker.getExtraInfo();
            if (null != bundle) {
                if (currentModuleType == EngineeringMainForManagerActivity.MODULE_ENGINEERING_MANAGEMENT) {
                    OrderParentBean orderParentBean = (OrderParentBean) bundle.getSerializable(ConstantUtil.KEY_1);

                    selectEngineering(orderParentBean.getLat(), orderParentBean.getLon());


                    BottomDialog dialog = new BottomDialog(getActivity(), orderParentBean);
                    dialog.setBottomDialogClick(MapForManagerFragment.this);
                    dialog.show();

                } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_TRACK) {


                } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_INTELLIGENT) {

                } else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_GONGXU) {

                }


            }

            return true;
        }
    };


    @Override
    public void onDialogClick(OrderParentBean orderParentBean) {
        if (null != orderParentBean) {
            CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
            Intent mIntent;
            if (CommonEnum.FeedBackState.EXCEPTION == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                        mIntent = new Intent(getActivity(), EngineeringFeedBackForExceptionStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivity(mIntent);

            } else if (CommonEnum.FeedBackState.WAIT_CONFIRM == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.WORKING == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                        mIntent = new Intent(getActivity(), EngineeringFeedBackForWorkingStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                startActivity(mIntent);

            } else if (CommonEnum.FeedBackState.COMPLETE == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                        mIntent = new Intent(getActivity(), EngineeringFeedBackForCompletionStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivity(mIntent);


            } else if (CommonEnum.FeedBackState.OUT_OF_PLAN == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.NO_FEEDBACK == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.PAUSE == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                        mIntent = new Intent(getActivity(), EngineeringFeedBackForPauseStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivity(mIntent);
            } else if (CommonEnum.FeedBackState.CREATE == feedBackState) {
                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                        mIntent = new Intent(getActivity(), EngineeringFeedBackForPauseStateActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
                startActivity(mIntent);
            }
//            Intent mIntent = new Intent(getActivity(), EngineeringInstallOrderForSuperActivity.class);
//            mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//            startActivity(mIntent);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getOrderList(currentModuleType);
                    break;

            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        bmapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        bmapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        bmapView.onDestroy();
    }


    @Override
    public void getEngineeringOrderListForMapFailed(String desc) {

    }

    @Override
    public void getEngineeringOrderListForMapSuccessed(List<OrderParentBean> allOrderList, List<OrderParentBean> maintenaceOrderList, List<OrderParentBean> engineeringManagerorderList) {
        this.engineeringOrderList = allOrderList;
        updataMap();
    }

    @Override
    public void getCompletionOrderListForMapSuccessed(List<OrderParentBean> allOrderList) {
        this.completionOrderList = allOrderList;
        updataMap();
    }

    @Override
    public void getCompletionOrderListForMapFailed(String desc) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


}

